﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using WatiN.Core;
using WatiN.Core.UtilityClasses;
using WebAutomation.Browsers;

namespace WebAutomation
{
    public class PictureCommand : CommandBase
    {
        public PictureCommand(WebBrowserObject browserObject)
        {
            this.Initialize(browserObject);

        }
        public override bool ExecuteCommand(WebBrowserObject browserObject, string[] parameter, string url, string testRepo, string testResultLocation, string dbConnString, ref System.Data.DataSet dataFromDB, ref TestLineResult testLineResult, ref bool stopTest, WebAutoUtils.ITestDataExceptionRules testDataExceptionRules)
        {

            string command = parameter[0];
            try
            {
                testLineResult.LineCommand = command;
                testLineResult.LineParameter = parameter[1];
                if (this.BrowserObject == null)
                {
                    this.BrowserObject = browserObject;
                }
                
                string imageFileName = testResultLocation + "\\" + parameter[1].Replace("\r\n", "") + ".jpg";
                int imageQuality = 20;
                try
                {
                    imageQuality = Convert.ToInt32(parameter[2]);
                }catch{}
                switch (command.ToLower())
                {
                    case "capture":


                        CaptureWebPageAsImage(imageFileName, imageQuality.ToString());
                        testLineResult.LinePassed = true;
                        break;

                }

            }
            catch (Exception ex)
            {
                testLineResult.IsFrameworkError = true;
                testLineResult.LineMessage = "Error." + ex.Message + "." + ex.StackTrace;
                testLineResult.LinePassed = false;
            }

            return testLineResult.LinePassed;

        }

        public virtual void CaptureWebPageAsImage(string imageFileName, string imageQualityString)
        {
            int imageQuality = 20;
            try
            {
                imageQuality = Convert.ToInt32(imageQualityString);
            }
            catch
            {

            }
            if (imageQuality > 100)
            {
                imageQuality = 100;
            }
            this.BrowserObject.CaptureWebPageAsImage(imageFileName, imageQualityString, imageQuality);
        

        }

    }
}
